/* Copyright (c) 2004-2023 Peigen.info. All rights reserved. */
package info.peigen.hotpot.manage.core.enums;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.annotation.JSONCreator;
import com.alibaba.fastjson2.annotation.JSONType;
import com.google.common.collect.Lists;
import info.peigen.hotpot.common.core.enums.Messageable;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.beetl.sql.annotation.entity.EnumValue;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <b>(ManageOrgCategory)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2024/6/11
 */
@SuppressWarnings("unused")
@Getter
@NoArgsConstructor
@AllArgsConstructor
@JSONType(writeEnumAsJavaBean = true)
public enum ManageOrgCategory implements Messageable {
    /** 为了对抗 Alibaba Java Coding 而存在的注释 */
    COMPANY("COMPANY", "公司"),
    DEPT("DEPT", "部门"),

    /** 不晓得啷格回事 */
    UNKNOWN("--", "不晓得啷格回事");

    /** 枚举值 */
    @EnumValue
    String code;

    /** 枚举描述 */
    String message;

    /**
     * 通过枚举<code>code</code>获得枚举
     *
     * @param code 枚举值
     *
     * @return ManageOrgCategory
     */
    public static ManageOrgCategory getByCode(String code) {
        return Stream.of(values())
                .filter(innerEnum -> StrUtil.equals(code, innerEnum.code()))
                .findFirst().orElse(UNKNOWN);
    }

    /**
     * 通过枚举<code>name</code>获得枚举
     *
     * @param name 枚举名
     *
     * @return ManageOrgCategory
     */
    public static ManageOrgCategory getByName(String name) {
        return Stream.of(values())
                .filter(innerEnum -> StrUtil.equalsIgnoreCase(name, innerEnum.name()))
                .findFirst().orElse(UNKNOWN);
    }

    /**
     * 获取一个随机枚举
     *
     * @return ManageOrgCategory
     */
    public static ManageOrgCategory getRandomEnum() {
        return RandomUtil.randomEle(Stream.of(values()).filter(v -> v != UNKNOWN).toList());
    }

    /**
     * 获取一个随机枚举
     *
     * @param ignores 忽略几个枚举
     *
     * @return ManageOrgCategory
     */
    public static ManageOrgCategory getRandomEnum(ManageOrgCategory... ignores) {
        return RandomUtil.randomEle(Stream.of(values()).filter(v -> !CollUtil.contains(Arrays.asList(ignores), v)).toList());
    }

    /**
     * 获取一堆随机枚举
     *
     * @return ManageOrgCategory
     */
    public static List<ManageOrgCategory> getRandomEnums(int count) {
        return RandomUtil.randomEleList(Stream.of(values()).filter(v -> v != UNKNOWN).toList(), count);
    }

    /**
     * 获取一堆随机枚举
     *
     * @param ignores 忽略几个个枚举
     *
     * @return List<$ { NAME }>
     */
    public static List<ManageOrgCategory> getRandomEnums(List<ManageOrgCategory> ignores, int count) {
        return RandomUtil.randomEleList(Stream.of(values()).filter(v -> !CollUtil.contains(ignores, v)).toList(), count);
    }

    @JSONCreator
    public static ManageOrgCategory deserialize(String code) {
        return Stream.of(values())
                .filter(innerEnum -> StrUtil.equals(code, innerEnum.code()))
                .findFirst().orElse(UNKNOWN);
    }

    /**
     * 获取全部枚举
     *
     * @return List<ManageOrgCategory>
     */
    public static List<ManageOrgCategory> getAllEnum() {
        return Lists.newArrayList(values());
    }

    /**
     * 获取全部枚举值
     *
     * @return List<String>
     */
    public static List<String> getAllEnumCode() {
        return getAllEnum().stream().map(ManageOrgCategory::code).collect(Collectors.toList());
    }

    /**
     * 判断给定的枚举，是否在列表中
     *
     * @param values 列表
     *
     * @return Boolean
     */
    public boolean isInList(ManageOrgCategory... values) {
        for (ManageOrgCategory e : values) {
            if (this == e) {
                return true;
            }
        }
        return false;
    }

    /**
     * 通过code获取msg
     *
     * @param code 枚举值
     *
     * @return String
     */
    public static String getMsgByCode(String code) {
        return getByCode(code).message();
    }

    /**
     * 获取枚举code
     *
     * @param innerEnum 枚举
     *
     * @return code
     */
    public static String getCode(ManageOrgCategory innerEnum) {
        return Optional.ofNullable(innerEnum).map(ManageOrgCategory::code).orElse(UNKNOWN.code());
    }

    /**
     * map
     *
     * @return Map
     */
    public static Map<String, String> maps() {
        return Stream.of(values()).collect(Collectors.toMap(ManageOrgCategory::code, ManageOrgCategory::message));
    }

    /**
     * 该方法是为了避免NPE，安全获取枚举中的字段值
     */
    public static <T> T safeGet(ManageOrgCategory enumValue, Function<ManageOrgCategory, T> mapper, T defaultValue) {
        return Objects.isNull(enumValue) ? defaultValue : mapper.apply(enumValue);
    }

    /**
     * 该方法是为了避免NPE，安全获取枚举中的字段值，默认返回值是null
     */
    public static <T> T safeGet(ManageOrgCategory enumValue, Function<ManageOrgCategory, T> mapper) {
        return safeGet(enumValue, mapper, null);
    }

    /**
     * 该方法是为了避免NPE，安全获取枚举中的code值，默认返回值是null
     */
    public static String safeGetCode(ManageOrgCategory enumValue) {
        return safeGet(enumValue, Messageable::code, null);
    }

    /**
     * @return Returns the code.
     */
    @Override
    public String code() {
        return code;
    }

    /**
     * @return Returns the message.
     */
    @Override
    public String message() {
        return message;
    }
}